<?php
 session_start();
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title>Parent Informer RCSHS - Login Attempt</title>

<meta http-equiv="Content-Type" content="text/html;charset=utf-8">

<link rel=stylesheet href="../css-layouts/themes/defaulttheme.css" type="text/css">
<link rel=stylesheet href="../css-layouts/headlinks.css" type="text/css">

</head>

<?php 

 require_once("../sqlconfig.php");

 mysql_connect($server, $sqlusr, $sqlpass) or die(mysql_error());
 mysql_select_db($mydb) or die(mysql_error()); 

 /*
  * This condition checks whether there were existing
  * cookies in the user's computer. If there is, the user
  * is redirected to the main page. If not, then the user
  * is redirected to the main login page.
  */
 if(isset($_COOKIE["parentinformerprcshs"]) && isset($_COOKIE["parentinformerurcshs"])) {
  $username = $_COOKIE["parentinformerprcshs"];
  $pass = $_COOKIE["parentinformerurcshs"];
  $check = mysql_query("SELECT * FROM logtbl WHERE usernm = '".$username."'") or die(mysql_error());
  while($info=mysql_fetch_array($check)) {
   //If stored cookie does not match the database (perhaps corrupted or modified)
   if ($pass!=$info['pwrnd']) {
    //Delete existing cookies
    setcookie("parentinformerprcshs", "", time()-3600,"/");
    setcookie("parentinformerurcshs","",time()-3600,"/");
    $_SESSION['theerrcode'] = 2;
   }
   else {
    header("Location: ../main.php");
   }
  }
 }
 else {

?>

<body>

<div id="mainhead"><br><br><br></div>
<center>
<br>
<br>
<div id="logintitle">&nbsp;<br><br><br><br><br><br><br></div>
<br>
</center>

<div class="notifymain">

<?php

   /*
    * Display the appropriate notice.
    *
    * Error codes (For use in $_SESSION['theerrcode'];)
    *
    * CODE 0: No error... xD
    * CODE 1: Username of password incorrect (regardless of type) :D
    * CODE 2: Incomplete cookies (regardless of type) :))
    * CODE 3: No username or password input (regardless of type) :p
    */
   $thecodeme = $_SESSION['theerrcode'];
   switch($thecodeme) {
    case 1:
     echo "<b>Incorrect Username or Password</b><br>";
     echo "The username or password you have inputted does not match the database.<br>";
     echo "Please try again.<br>";
     break;
    case 2:
     echo "<b>Cookies Cleared</b><br>";
     echo "The site cannot retrieve the full set of cookies in your computer.<br>";
     echo "Please log in again to access your page.<br>";
     break;
    case 3:
     echo "<b>No Username or Password Input</b><br>";
     echo "You did not enter any username and/or password in the previous page.<br>";
     echo "Please fill in all required fields to enter.<br>";
     break;
    default:
     echo "";
    }

?>

</div>
<br><br><br><br><br>

<form method=post action="verify.php">
<table align=center id="labelforms">

<tr>
<td>Username:</td>
<td><input type="text" name="usrnm"></td>
</tr>

<tr>
<td>Password:</td>
<td><input type="password" name="paswrd"></td>
</tr>

<tr>
<td></td>
<td><input type="submit" name="submitbtn" Value="Login"></td>
</tr>

</table>
</form>

<div class="footnav" align=center>

<hr>

<b>Main Page</b> ||
<a href="../externalaffairs/newaccnt.php">Request Account</a> ||
<a href="#">Help</a> ||
<a href="#">Disclaimer</a> ||
<a href="../externalaffairs/links.php">Links</a> ||
<a href="#">About</a>
<br>

<span id="copyme">
&copy; 2011 Roosevelt College Science High School. All rights reserved.
</span>

</div>

</body>

<?php

 }

?>

</html>